﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Service;

namespace community
{
    public partial class familiesin : Form
    {
        public static string JMBH;
        public static string JTBH;
        public familiesin()
        {
            InitializeComponent();
        }

        private void familiesin_Load(object sender, EventArgs e)
        {
            textBox1.Text = family.communityMC;
            textBox2.Text = family.familyHZXM;
            textBox3.Text = family.familyBH;
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
            textBox7.Text = "";
            textBox8.Text = "";
            textBox9.Text = "";
            textBox10.Text = "";
            textBox11.Text = "";
            comboBox1.Text = "";
        }

        private void linkLabel1_Click(object sender, EventArgs e)
        {
            residentcheck rc = new residentcheck();
            rc.ShowDialog();
            JMBH = residentcheck.resident_JMBH;
            
            if (JMBH == "")
                return;
            Cls_DBHelper help = new Cls_DBHelper();
            string str = "select residentname_chr as 居民姓名,inhabitantcode_chr as 居民编号,communityname_chr as 所属社区,sex_chr as 居民性别,birth_dat as 出生日期,identitycard_chr as 身份证号,careerstyle_chr as 居民职业,phone_chr as 本人电话 from T_CS_INHABITANTRECORD_NEW where inhabitantcode_chr='" + JMBH + "'";
            SqlDataReader dr = help.m_clsGetReader(str);

            if (dr.Read())
            {
                textBox4.Text = dr["居民姓名"].ToString();
                textBox5.Text = dr["居民编号"].ToString();
                textBox6.Text = dr["所属社区"].ToString();
                textBox7.Text = dr["居民性别"].ToString();
                textBox8.Text = dr["出生日期"].ToString();
                textBox9.Text = dr["身份证号"].ToString();
                textBox10.Text = dr["居民职业"].ToString();
                textBox11.Text = dr["本人电话"].ToString();


                //DataTable table = help.m_clsGetDataSet("select a.inhabitantcode_chr as 居民编号,a.residentname_chr as 居民姓名,a.identitycard_chr as 身份证号,a.sex_chr as 性别,a.birth_dat as 出生日期,a.rehouseholder_vchr as 与户主关系 from T_CS_INHABITANTRECORD_NEW as a where a.familycode_chr =" + dr["家庭编号"].ToString());
                //if (table.Rows.Count <= 6)
                //{
                //    for (int ii = table.Rows.Count; ii <= 6; ii++)
                //    {
                //        DataRow newRow = table.NewRow();
                //        table.Rows.Add(newRow);
                //    }
                //}
                dr.Close();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(Convert.ToString ( textBox4.Text)=="")
            {
                MessageBox.Show("请输入要加入的家庭成员", "提示", MessageBoxButtons.OKCancel);
                return;
            }
            //if (Convert.ToString(residentcheck.resident_JTBH) != "")
            //{
            //    MessageBox.Show("此居民已加入家庭，请重新检索要加入的家庭成员", "提示", MessageBoxButtons.OKCancel);
            //    return;
            //}
            if (Convert.ToString(comboBox1.Text) == "")
            {
                MessageBox.Show("请输入与户主关系", "提示", MessageBoxButtons.OKCancel);
                comboBox1.Focus();
                return;
            }

            //string str = "update T_CS_INHABITANTRECORD_NEW  set familycode_chr='" + Convert.ToString (textBox3.Text.Trim()) + "',rehouseholder_vchr='" + JMBH + "' where inhabitantcode_chr='" + Convert.ToString(comboBox1.Text.Trim()) + "'";
            string str = "update T_CS_INHABITANTRECORD_NEW  set familycode_chr='" + Convert.ToString(textBox3.Text.Trim()) + "', rehouseholder_vchr='" + Convert.ToString(comboBox1.Text.Trim()) + "' where inhabitantcode_chr='" + JMBH + "'";
            JTBH = Convert.ToString(textBox3.Text.Trim());
            SqlConnection  cn = new SqlConnection(SQL.conStr);
            SqlCommand com = new SqlCommand(str, cn);
            cn.Open();
            com.ExecuteNonQuery();
            cn.Close();
            MessageBox.Show("加入家庭成功", "提示", MessageBoxButtons.OKCancel);
            this.Close();
        }
    }
}
